﻿using MFG.DotNetCore.Web.Models;
using MFG.Web.Common;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MFG.Web.Controllers
{
    public class PageController : Controller
    {

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Cases()
        {
            return View();
        }
        public ActionResult CaseDetail(string id)
        {

            if (!CaseInfo.Cache.ContainsKey(id)) return NotFound();
            ViewBag.caseInfo = CaseInfo.Cache[id];
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }

    public class CaseInfo
    {
        public string Title { get; set; }

        public string Id { get; set; }

        public string Thumb { get; set; }

        public List<string> Images { get; set; }

        static CaseInfo()
        {
           
            var text = File.ReadAllText(Path.Combine(Environments.BaseDirctory, "./Assets/resources/cases.json"));
            var list = JsonConvert.DeserializeObject<List<CaseInfo>>(text);
            cache = new Dictionary<string, CaseInfo>(list.Count);
            for (int i = list.Count - 1; i >= 0; i--)
            {
                var item = list[i];
                item.Id = (i).ToString();
                cache.Add(item.Id, item);
            }
        }
        private static Dictionary<string, CaseInfo> cache;
        public static Dictionary<string, CaseInfo> Cache => cache;
    }
}
